#include <ESP8266WiFi.h>
#include <ESP8266httpUpdate.h>
#include <Ticker.h>
 

#define wifi_name       "laptop_zeeland_5592"
#define wifi_password   "12345678"
#define VERSION        "1.02"
String upUrl = "http://bin.bemfa.com/b/3BcYzBhNWZlNWU0NDhmM2UwOWQ1NTRmODUzZDhiMWZkZjU=interval1s.bin";
const int blinkInterval = 1; 
Ticker ticker;
int workCount = 0;

void tickerCount(){
  Serial.print("LED COUNT");
  Serial.println(workCount++);
  digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}

void updateBin(){
  WiFiClient UpdateClient;
  t_httpUpdate_return ret = ESPhttpUpdate.update(UpdateClient, upUrl,VERSION);
  switch(ret) {
    case HTTP_UPDATE_FAILED:      //当升级失败
        Serial.println("[update] Update failed.");
        break;
    case HTTP_UPDATE_NO_UPDATES:  //当无升级
        Serial.println("[update] Update no Update.");
        break;
    case HTTP_UPDATE_OK:         //当升级成功
        Serial.println("[update] Update ok.");
        break;
  }
}


void setup() {
  Serial.begin(115200);                    
  Serial.println("begin to work,current version:" + String(VERSION));
  pinMode(LED_BUILTIN, OUTPUT);
  ticker.attach(blinkInterval, tickerCount);  // 设置Ticker对象
  WiFi.begin(wifi_name, wifi_password);    
  while (WiFi.status() != WL_CONNECTED) {  
    delay(500);
    Serial.print(".");
  }
  Serial.println("WIFI has connected");
  Serial.println("start update");
  ticker.attach(blinkInterval, tickerCount);  // 设置Ticker对象

  updateBin();                            
}

void loop() {

}


